/*
 * Copyright 2004-2006 asoft
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.asoft.cn/licenses
 */

package com.asoft.common.sysframe.dao;

import java.util.List;

import org.apache.log4j.Logger;

import com.asoft.common.base.dao.BaseDAOHibernate;
import com.asoft.common.sysframe.model.Post;
import com.asoft.common.sysframe.util.AsoftSysFrameException;

/**
 * <p>Title: 岗位</p>
 * <p>Description: 岗位 Post </p>
 * <p>Copyright: Copyright (c) 2004-2006</p>
 * <p>Company: asoft</p>
 * @ $Author: amon.lei $
 * @ $Date: 2007-2-20 $
 * @ $Revision: 1.0 $
 * @ created in 2007-2-20
 *
 */

public class PostDAOimp extends BaseDAOHibernate
                             implements PostDAO
{
        static Logger logger = Logger.getLogger(PostDAOimp.class);

        /**
         * 描述:岗位
         *
         * @ param:
         * @ param:orderBy 排序属性（for hql）
         * @ param:sortType 排序方式（asc or desc）
         * @ Exception:
         * @ return model集合
         */
        public List findPosts(String orgId,
                              String roleId,
                              String orderBy,
                              String sortType)

        {
                String hql = " from Post post " + 
               " where( " +   
               "     post.org.id like ? " + 
               " and post.role.id like ? " + 
               " ) " + 
                " order by " + orderBy + " " + sortType;
                Object[] values = new Object[2];
                values[0] = orgId;
                values[1] = roleId;
                return this.getHibernateTemplate().find(hql,values);
        }

        public Post getByOrgIdNRoleId(String orgId, String roleId)
        {
                logger.debug("查询岗位by机构Id和角色Id");
                String hql = " from Post post " + 
                 " where( " +   
                 "     post.org.id = ? " + 
                 " and post.role.id = ? " + 
                 " ) " + 
                 " order by post.org.pri,post.role.pri asc ";
                 Object[] values = new Object[2];
                 values[0] = orgId;
                 values[1] = roleId;
                 List posts = this.getHibernateTemplate().find(hql,values);
                 logger.debug("岗位个数： " + posts.size());
                 if(posts.size() == 0)
                 {
                         return null;
                 }
                 if(posts.size() > 1)
                 {
                         throw new AsoftSysFrameException("岗位重复(orgId: " + orgId + " roleId:" + roleId + ")" );
                 }
                 return (Post)posts.get(0);
        }
}
